package xyz.longicorn.driver.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import lombok.SneakyThrows;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.longicorn.driver.dto.ApiResult;
import xyz.longicorn.driver.dto.LoginModel;
import xyz.longicorn.driver.dto.LoginUserInfo;
import xyz.longicorn.driver.pojo.LoginUser;
import xyz.longicorn.driver.pojo.UserInfo;
import xyz.longicorn.driver.service.UserService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserService userService;

    // @Validated用于验证字段的值
    // @RequestBody 只能Post请求且 content-type=>application/json
    @SneakyThrows
    @PostMapping("/login")
    public ApiResult login(@Validated @RequestBody LoginModel model) {
        // 判断是否有验证码数据 有则需要判断验证码是否正确
        if (StringUtils.hasLength(model.getCodeId())) {
            String key = "code:" + model.getCodeId();
            String code1 = stringRedisTemplate.opsForValue().get(key);

            if (code1 == null || !code1.equals(model.getCode())) {
                return ApiResult.error(1, "验证码无效");
            }
            // 使原始验证码失效
            stringRedisTemplate.delete(key);
        }

        Thread.sleep(2);
        final LoginUser user = userService.login(model.getUsername(), model.getPassword());

        return ApiResult.success(user);
    }

    @PostMapping("/reg")
    public ApiResult reg(@RequestBody LoginUserInfo userInfo) {
        String code = stringRedisTemplate.opsForValue().get("email:code:" + userInfo.getEmail());
        if(code == null || !code.equalsIgnoreCase(userInfo.getCode())){
            return ApiResult.error(1,"验证码有误");
        }
        userService.signup(userInfo);
        return ApiResult.success(userInfo);
    }

    @RequestMapping("/info")
    public UserInfo loginUserInfo() {
        return (UserInfo) StpUtil.getSession().get("user");
    }

    /**
     * 验证邮箱是否已经存在 不存在返回true 存在false
     *
     * @param email
     * @return
     */
    @RequestMapping("/check-email")
    public ApiResult checkEmail(String email) {
        return ApiResult.success(
                userService.getByEmail(email) == null
        );
    }

    @RequestMapping("/check-account")
    public ApiResult checkAccount(String account) {
        return ApiResult.success(
                userService.getByAccount(account) == null
        );
    }
    /*
     注册的邮箱验证有2中方案
     1.填写验证码后才能提交注册(优点：不需要单独的激活业务 缺点：用户不一定能够及时收到正确验证码)
        -- 需要单独的业务同步发

     2.直接注册 后台发送一封带有验证链接的邮件 (优点: 对于激活的时效性不需要太高(用消息队列 让其他服务发送) 缺点：必须有专门的激活业务,用户可能不会激活)
        -- 在注册完成后异步发送（推荐使用消息队列）
     */

}
